package com.eduask.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.eduask.beans.Client;
import com.eduask.dao.ClientDAO;
import com.eduask.util.HibernateUtil;

@Repository
public class ClientDAOImpl extends HibernateUtil implements ClientDAO{

	@Override
	public void saveClient(Client client) {
		getSession().save(client);
	}

	@Override
	public void removeClient(Client client) {
		getSession().delete(client);
	}

	@Override
	public Client findClientById(Integer id) {
		return getSession().get(Client.class, id);
	}

	@Override
	public List<Client> findAllClients() {
		String hql = "from Client client order by client.id asc";
		return getSession().createQuery(hql).list();
	}

	@Override
	public void updateClient(Client client) {
		getSession().saveOrUpdate(client);
	}

	@Override
	public List<Client> login(String name, String password){
		String hql = "from Client client where client.name= ? and client.password= ?";
		Query query = getSession().createQuery(hql);
		query.setParameter(0, name);
		query.setParameter(1, password);
		return query.list();
	}
}
